﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Claims;
using System.Threading.Tasks;

namespace TL_Cloud.API.Controllers
{
    public static class ControllerHelper
    {
        public static Tuple<string, string> GetUserInfo(this ControllerBase controller)
        {
            if (controller.User == null)
            {
                return new Tuple<string, string>("", "");
            }
            else
            {
                if (controller.User.Claims == null)
                {
                    return new Tuple<string, string>("", "");
                }
                else
                {
                    string userId = "";
                    string userName = "";
                    foreach (var item in controller.User.Claims)
                    {
                        if (item.Type == ClaimTypes.NameIdentifier)
                        {
                            userId = item.Value;
                        }
                        else if (item.Type == ClaimTypes.Name)
                        {
                            userName = item.Value;
                        }
                    }
                    return new Tuple<string, string>(userId, userName);
                }
            }
        }
    }
}
